<template>
  <div class="outside">
    <div class="app-container" style="width: 92%">
      <inline-form
        ref="InlineForm"
        :item="item"
        @doSubmit="handleDoSubmit"
      ></inline-form>
    </div>
  </div>
</template>

<script>
import _form from "./_form";
import { addImage } from "../../../../utils/common";
import { queryItem, priceEdit, edit } from "./api";
export default {
  data() {
    return {
      id: null,
      item: {
        productName: "",
        productImage: "",
        productImageFile: "img_none",
        productDetailImage: "",
        productDetailImageFile: "img_none",
        jifen: "",
        jifenId: "",
        count: "",
        price: "",
        priceId: "",
        jifenAndPrice: "",
        jifenAndPriceId: "",
      },
      updateItemFunc: (target) => {
        this.item = {
          productName: target.productName,
          productImage: target.productImage,
          productImageFile: "img_none",
          productDetailImage: target.productDetailImage,
          productDetailImageFile: "img_none",
          jifen: target.jifen,
          jifenId: target.jifenId,
          count: target.count,
          price: target.price,
          priceId: target.priceId,
          jifenAndPrice: target.jifenAndPrice,
          jifenAndPriceId: target.jifenAndPriceId,
          list: target.list,
          productId: target.productId,
        };
      },
    };
  },
  created() {
    this.id = this.$route.params.id;
    this.get_item();
  },
  methods: {
    get_item() {
      if (this.id) {
        // 查
        queryItem(this.id).then((data) => {
          console.log(data);
          if (data.code === 1) {
            let act = data.data.product;
            data.data.list.forEach((t) => {
              if (t.jifen === 0) {
                act.price = t.price;
                act.priceId = t.id;
              }
              if (t.price === 0) {
                act.jifen = t.jifen;
                act.jifenId = t.id;
              }
              if (t.price !== 0 && t.jifen !== 0) {
                act.jifenAndPrice = `${t.jifen}+${t.price}`;
                act.jifenAndPriceId = t.id;
              }
            });
            act.list = data.data.list;
            this.updateItemFunc(act);
            console.log(this.item, "修改后的");
          } else {
            this.$message.error(data.msg);
          }
        });
      } else {
        $this.$message.error("数据ID不存在，请检查后重写");
      }
    },
    // 修改
    async handleDoSubmit() {
      let form = this.$refs.InlineForm;
      form.valid(async (valid) => {
        if (valid) {
          let item = form.obj;
          console.log(item, "修改");
          if (
            item.productImageFile &&
            item.productImageFile !== "" &&
            item.productImageFile !== "img_none"
          ) {
            item.productImage = await addImage(item.productImageFile);
          }
          if (
            item.productDetailImageFile &&
            item.productDetailImageFile !== "" &&
            item.productDetailImageFile !== "img_none"
          ) {
            item.productDetailImage = await addImage(
              item.productDetailImageFile
            );
          }
          delete item.productImageFile;
          delete item.productDetailImageFile;
          console.log(item, "=====)))))))00000");
          const res = await edit(item);
          console.log(res, "====>");
          if (res.code === 1) {
            try {
              await priceEdit(
                this.id,
                `jifen=${item.jifen}&id=${item.jifenId}`
              );
              await priceEdit(
                this.id,
                `price=${item.price}&id=${item.priceId}`
              );
              await priceEdit(
                this.id,
                `jifen=${item.jifenAndPrice.split("+")[0]}&price=${
                  item.jifenAndPrice.split("+")[1]
                }&id=${item.jifenAndPriceId}`
              );
              this.$message.success("修改成功!");
              this.$router.go(-1);
            } catch (e) {
              console.log(e);
              this.$message.error(e);
            }
          } else {
            this.$message.error(res.msg);
          }
        } else {
          this.$message.error("数据填写错误，请按要求填写!");
        }
      });
    },
  },
  components: {
    "inline-form": _form,
  },
};
</script>

<style scoped>
</style>
